package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.stock.EmployeeTaskBo;
import com.ruoyi.wms.domain.entity.stock.EmployeeTask;
import com.ruoyi.wms.domain.vo.EmployeeTaskVo;
import com.ruoyi.wms.mapper.EmployeeTaskMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 员工任务Service业务层处理
 *
 * @author xhr
 * @date 2024-11-26
 */
@RequiredArgsConstructor
@Service
public class EmployeeTaskService {

    private final EmployeeTaskMapper employeeTaskMapper;

    /**
     * 查询员工任务
     */
    public EmployeeTaskVo queryById(Long id){
        return employeeTaskMapper.selectVoById(id);
    }

    /**
     * 查询员工任务列表
     */
    public TableDataInfo<EmployeeTaskVo> queryPageList(EmployeeTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmployeeTask> lqw = buildQueryWrapper(bo);
        Page<EmployeeTaskVo> result = employeeTaskMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工任务列表
     */
    public List<EmployeeTaskVo> queryList(EmployeeTaskBo bo) {
        LambdaQueryWrapper<EmployeeTask> lqw = buildQueryWrapper(bo);
        return employeeTaskMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmployeeTask> buildQueryWrapper(EmployeeTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmployeeTask> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEmployeeId() != null, EmployeeTask::getEmployeeId, bo.getEmployeeId());
        lqw.eq(bo.getStatus() != null, EmployeeTask::getStatus, bo.getStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            EmployeeTask::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增员工任务
     */
    public void insertByBo(EmployeeTaskBo bo) {
        EmployeeTask add = MapstructUtils.convert(bo, EmployeeTask.class);
        employeeTaskMapper.insert(add);
    }

    /**
     * 修改员工任务
     */
    public void updateByBo(EmployeeTaskBo bo) {
        EmployeeTask update = MapstructUtils.convert(bo, EmployeeTask.class);
        employeeTaskMapper.updateById(update);
    }

    /**
     * 批量删除员工任务
     */
    public void deleteByIds(Collection<Long> ids) {
        employeeTaskMapper.deleteBatchIds(ids);
    }
}
